<template>
	<view class="">
		<liu-chooseAddress ref="liuChooseAddress" @submit="submitAddress" @detele="deteleAddress">
		</liu-chooseAddress>
	</view>
</template>

<script>
	import {
		add,
		insert,
		update,
		queryList,
		delAddress,
		detail,
		address
	} from '@/api/address.js';
	export default {
		data() {
			return {
				pageNum:1,
				pageSize:5,
				userId:1,
				is_default:0,
				id:'',
				cart_ids:'',
			}
		},
		onLoad(e) {
			let _this = this;
			
			console.log(e)
			if(e.cart_ids){
				_this.cart_ids = e.cart_ids;
			}
			if(e.isType){
				_this.isType = e.isType;
			}
			if(e.id){
				_this.id = e.id;
				_this.editAddress();
				uni.setNavigationBarTitle({
					title: "编辑地址"
				});
			}else{
				_this.inletType = e.inletType;
				uni.setNavigationBarTitle({
					title: "添加地址"
				});
				
			}
		},
		
		methods: {
			//添加地址成功回调
			submitAddress(e) {
				console.log(e)
				let _this = this;
				e.itemCode = _this.mallId;
				e.userId = _this.userId;
				let phone_regular = /^1[3456789]\d{9}$/;
				if(e.userPhone == ""){
					uni.showToast({
						title: '请输入您的手机号！',
						icon: "none"
					});
					return false;
				}else if(!(phone_regular.test(e.userPhone))) {
					uni.showToast({
						title: '手机号码有误，请重填',
						icon: "none"
					});
					return false;
				}
				// _this.getUpdate(e)
				if(_this.id == ''){
					_this.getInsert(e)
					
				}else{
					e.id = _this.id;
					_this.getUpdate(e)
				}
				
			},
			//新增保存
			getInsert(e){
				console.log(e)
				let _this = this;
				if(e.agreeState){
					_this.is_default = 1;
				}else{
					_this.is_default = 0;
				}
				let data ={
					recipient:e.userName,
					phone_number:e.userPhone,
					province:1,
					city:1,
					district:1,
					address:e.address + '-'+ e.title,
					is_default:_this.is_default,
					extra_field1:e.detailAddress,
					extra_field2:e.longitude,
					extra_field3:e.latitude,
				}
				add(data).then(res => {
					
					if(res.code == 200){
						
						
						uni.showModal({
							title: '温馨提示',
							content: res.message,
							showCancel:false,
							success: function (e) {
								if (e.confirm) {
									
									if(_this.cart_ids){
										uni.reLaunch({
											url: "/package/addOrder/index?cart_ids="+_this.cart_ids+"&address_id="+res.data.id
										});
									}else{
										uni.redirectTo({
											url: '/package/address/index'
										});
									}
								}
							}
						});
						
						
						
					}
				})
			},
			//修改保存
			getUpdate(e){
				console.log(e)
				let _this = this;
				if(e.agreeState){
					_this.is_default = 1;
				}else{
					_this.is_default = 0;
				}
				let data ={
					id:_this.id,
					recipient:e.userName,
					phone_number:e.userPhone,
					province:1,
					city:1,
					district:1,
					address:e.address,
					is_default:_this.is_default,
					extra_field1:e.detailAddress,
					extra_field2:e.longitude,
					extra_field3:e.latitude,
				}
				update(data).then(res => {
					console.log(res)
					if(res.code == 200){
						uni.showModal({
							title: '温馨提示',
							content: res.message,
							showCancel:false,
							success: function (e) {
								if (e.confirm) {
									
									if(_this.cart_ids){
										uni.reLaunch({
											url: "/package/addOrder/index?cart_ids="+_this.cart_ids+"&address_id="+_this.id
										});
									}else{
										uni.reLaunch({
											url: '/package/address/index'
										});
									}
								}
							}
						});
						
						
					}
				})
			},
			//删除地址回调
			deteleAddress(e) {
				let _this = this;
				if(this.id == ''){
					uni.showToast({
						title: '请先保存地址！',
						icon: "none"
					});
					return;
				}
				let data = {id:this.id}
				delAddress(data).then(res => {
					
					if(res.code == 200){
						uni.showModal({
							title: '温馨提示',
							content: res.message,
							showCancel:false,
							success: function (e) {
								if (e.confirm) {
									if(_this.cart_ids){
										uni.reLaunch({
											url: "/package/addOrder/index?cart_ids="+_this.cart_ids
										});
									}else{
										uni.reLaunch({
											url: '/package/address/index'
										});
									}
									// uni.redirectTo({
									// 	url: '/pages/address/index?mall_id='+_this.mall_id+'&inletType=1'
									// });
								}
							}
						});
						
					}
				})
			},
			// 地址回显(编辑)调用此方法
			editAddress() {
				let _this = this;
				let data = {
					address_id:_this.id
				}
				detail(data).then(res => {
					console.log(res)
					let a = '';
					if(res.data.is_default == 1){
						a = true;
					}else{
						a = false;
					}
					let data = {
						userName:res.data.recipient,
						userPhone:res.data.phone_number,
						is_default:res.data.is_default,
						address:res.data.address,
						detailAddress:res.data.extra_field1,
						longitude:res.data.extra_field2,
						latitude:res.data.extra_field3,
						agreeState:a,
					}
					console.log(data)
					
					
					this.$refs.liuChooseAddress.setData(data)
				})
				
			}
		}
	}
</script>

<style>
</style>